# dns.tcl - dns external code
#
# CONTENTS:
#	dnscallback {token}
#		ONLY USE THIS VIA A DNS RESOLVE COMMAND. Acts
#		as callback to a [dns::resolve] command, shows
#		the result of the resolve.

proc dnscallback {token} {
	global conns n_conns
	
	set server [$n_conns tab [$n_conns select] -text]
	set stream $conns($server)
	global CHANS_$stream
	set currservertab $n_conns.$stream
	set channel [$currservertab tab [$currservertab select] -text]	
	set view [getchan]
	
	if { $channel != "Console" } {
		set num [set "CHANS_$stream\([string tolower $channel]\)"]
		set view $currservertab.chan$num
	}
	
	# Check for errors
	set errmsg [::dns::error $token]
	if [string length $errmsg] {
		errmsg $view "[tr \"Error resolving query\"]: $errmsg\n"
		return 1
	}
	
	# Now, output the hostnames and addresses got
	set idx_h 0
	foreach host [::dns::name $token] {
		incr idx_h
		infomsg $view "[tr Hostname] $idx_h: $host\n"
	}
	
	set idx_a 0
	foreach addr [::dns::address $token] {
		incr idx_a
		infomsg $view "[tr IP-address] $idx_a: $addr\n"
	}
	
	notice $view "[tr End of DNS query], $idx_h [tr §hostnames and] $idx_a [tr addresses found]\n"
}
